#define __IOPORT_C__
#include "ioport.h"
/*
function: ioport initialize
*/

void ioport_init(void)
{
	/*P40--P47 is AD input P4.6 P4.5 unused*/
	PORT4.ICR.BYTE=0x9f;
	/*P70 is OVP1 input*/
	PORT7.DDR.BIT.B0=0;
	PORT7.ICR.BIT.B0=1;
/*P73,6 is PWM1  output=0*/
	PORT7.DR.BIT.B3=0;
	PORT7.DDR.BIT.B3=1;
	PORT7.DR.BIT.B6=0;
	PORT7.DDR.BIT.B6=1;
/*PD0,1 is PWM1  output=0*/
	PORTD.DR.BIT.B0=0;
	PORTD.DDR.BIT.B0=1;
	PORTD.DR.BIT.B1=0;
	PORTD.DDR.BIT.B1=1;
	/*P73 is R_CON output=0*/
//	PORT7.DR.BIT.B3=0;
//	PORT7.DDR.BIT.B3=1;
PORT3.DR.BIT.B3=0;
PORT3.DDR.BIT.B3=1;
//	PORT7.DDR.BIT.B3=1;
	/*P80 is RXD2 input*/
	PORT8.ICR.BIT.B0=1;
	/*P81 is TXD2 output=1*/
	IOPORT.PFFSCI.BIT.SCI2S=1;
	/*P90--P93 is led output =0*/
	PORT9.DR.BIT.B0=1;
	PORT9.DDR.BIT.B0=1;
	PORT9.ICR.BIT.B0=1;
	PORT9.DR.BIT.B1=1;
	PORT9.DDR.BIT.B1=1;
	PORT9.ICR.BIT.B1=1;
	PORT9.DR.BIT.B2=1;
	PORT9.DDR.BIT.B2=1;
	PORT9.ICR.BIT.B2=1;
	PORT9.DR.BIT.B3=1;
	PORT9.DDR.BIT.B3=1;
	PORT9.ICR.BIT.B3=1;
	/*PA0--PA3 is key input */
	PORTA.ICR.BIT.B0=1;
	PORTA.ICR.BIT.B1=1;
	PORTA.ICR.BIT.B2=1;
	PORTA.ICR.BIT.B3=1;
	/*PB1 is IIC SCL*/
    PORTB.ICR.BIT.B1=1;
	PORTB.DDR.BIT.B1=1;
//	PORTB.DR.BIT.B1=0;
	/*PB2 is IIC SDA*/
    PORTB.ICR.BIT.B2=1;
	PORTB.DDR.BIT.B2=1;
//	PORTB.DR.BIT.B2=0;
	IOPORT.PFCMTU.BIT.MTUS1=1;
//	IOPORT.PFCMTU.BIT.MTUS0=1;
	
}

U8 get_keyport(void)
{
	if(!PORTA.PORT.BIT.B0)return 1;
	if(!PORTA.PORT.BIT.B1)return 2;
	if(!PORTA.PORT.BIT.B2)return 3;
	if(!PORTA.PORT.BIT.B3)return 4;
	return 0xff;
}

void led_on(U8 num)
{
/*	switch(num)
	{
		case 1:PORT9.DR.BIT.B0=0;return ;
		case 2:PORT9.DR.BIT.B1=0;return ;
		case 3:PORT9.DR.BIT.B2=0;return ;
		case 4:PORT9.DR.BIT.B3=0;return ;
	}
*/
}
void led_off(U8 num)
{
/*	switch(num)
	{
		case 1:PORT9.DR.BIT.B0=1;return ;
		case 2:PORT9.DR.BIT.B1=1;return ;
		case 3:PORT9.DR.BIT.B2=1;return ;
		case 4:PORT9.DR.BIT.B3=1;return ;
	}
*/
}
void led_roll(U8 num)
{
/*	switch(num)
	{
		case 1:
		{
			if(PORT9.PORT.BIT.B0)
			{
				PORT9.DR.BIT.B0=0;
			}
			else
			{
				PORT9.DR.BIT.B0=1;
			}
		}
		break;
		case 2:
		{
			if(PORT9.PORT.BIT.B1)
			{
				PORT9.DR.BIT.B1=0;
			}
			else
			{
				PORT9.DR.BIT.B1=1;
			}
		}
		break;
		case 3:
		{
			if(PORT9.PORT.BIT.B2)
			{
				PORT9.DR.BIT.B2=0;
			}
			else
			{
				PORT9.DR.BIT.B2=1;
			}
		}
		break;
		case 4:
		{
			if(PORT9.PORT.BIT.B3)
			{
				PORT9.DR.BIT.B3=0;
			}
			else
			{
				PORT9.DR.BIT.B3=1;
			}
		}
		break;
		
	}
*/
}


void led_on_v1(U8 num)
{
	switch(num)
	{
		case 1:PORT9.DR.BIT.B0=0;return ;
		case 2:PORT9.DR.BIT.B1=0;return ;
		case 3:PORT9.DR.BIT.B2=0;return ;
		case 4:PORT9.DR.BIT.B3=0;return ;
	}
}
void led_off_v1(U8 num)
{
	switch(num)
	{
		case 1:PORT9.DR.BIT.B0=1;return ;
		case 2:PORT9.DR.BIT.B1=1;return ;
		case 3:PORT9.DR.BIT.B2=1;return ;
		case 4:PORT9.DR.BIT.B3=1;return ;
	}
}
void led_roll_v1(U8 num)
{
	switch(num)
	{
		case 1:
		{
			if(PORT9.PORT.BIT.B0)
			{
				PORT9.DR.BIT.B0=0;
			}
			else
			{
				PORT9.DR.BIT.B0=1;
			}
		}
		break;
		case 2:
		{
			if(PORT9.PORT.BIT.B1)
			{
				PORT9.DR.BIT.B1=0;
			}
			else
			{
				PORT9.DR.BIT.B1=1;
			}
		}
		break;
		case 3:
		{
			if(PORT9.PORT.BIT.B2)
			{
				PORT9.DR.BIT.B2=0;
			}
			else
			{
				PORT9.DR.BIT.B2=1;
			}
		}
		break;
		case 4:
		{
			if(PORT9.PORT.BIT.B3)
			{
				PORT9.DR.BIT.B3=0;
			}
			else
			{
				PORT9.DR.BIT.B3=1;
			}
		}
		break;
		
	}
}


void realy_con_h(void)
{
//	GPT2.GTIOR.BIT.OADFLT = 1;
PORT3.DR.BIT.B3=1;
}
void realy_con_l(void)
{
//	GPT2.GTIOR.BIT.OADFLT = 0;
PORT3.DR.BIT.B3=0;
}
